/**
 * Anderground project - Subway maps processor application.
 * Copyright (C) 2008-2009 Eldar Abusalimov, Ilya Shurigyn
 * 
 * This file is part of Anderground.
 * 
 * Anderground is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Anderground is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Anderground.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anderground.modules.drawer.dynamic;

import java.util.ArrayList;

import org.anderground.core.graphics.Canvas;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 * 
 */
class Road {

	@SuppressWarnings("unused")
	private Crossing evenCrossing, oddCrossing;

	/** the Bacillus to call the draw method */
	private Bacillus rootBacillus;

	/** array of primitive Bacilli that make up this Road */
	private ArrayList<SimpleBacillus> primitiveBacilli = new ArrayList<SimpleBacillus>();

	/**
	 * @param evenCrossing
	 * @param oddCrossing
	 * @param structure
	 *            TODO
	 */
	public Road(Crossing evenCrossing, Crossing oddCrossing, Structure structure) {
		this.evenCrossing = evenCrossing;
		this.oddCrossing = oddCrossing;
		evenCrossing.addNeighbor(oddCrossing, this);
		oddCrossing.addNeighbor(evenCrossing, this);
		structure.addRoad(this);
	}

	public void build() {
		ArrayList<Bacillus> bacilli = new ArrayList<Bacillus>(primitiveBacilli);

		while (bacilli.size() > 1
				|| bacilli.get(0).evenAmoeba.parentAmoeba != null
				|| bacilli.get(0).oddAmoeba.parentAmoeba != null) {
			Bacillus bacillus = null;
			Amoeba masterAmoeba = null;
			Amoeba slaveAmoeba = null;
			CompoundAmoeba minCompoundAmoeba = null, compoundAmoeba;
			for (Bacillus eachBacillus : bacilli) {
				if (eachBacillus.evenAmoeba.parentAmoeba != null) {
					compoundAmoeba = eachBacillus.evenAmoeba.parentAmoeba;
					if (minCompoundAmoeba == null
							|| minCompoundAmoeba
									.compareTo(compoundAmoeba) > 0) {
						minCompoundAmoeba = compoundAmoeba;
						bacillus = eachBacillus;
						masterAmoeba = bacillus.evenAmoeba;
						slaveAmoeba = bacillus.oddAmoeba;
					}
				}
				if (eachBacillus.oddAmoeba.parentAmoeba != null) {
					compoundAmoeba = eachBacillus.oddAmoeba.parentAmoeba;
					if (minCompoundAmoeba == null
							|| minCompoundAmoeba
									.compareTo(compoundAmoeba) > 0) {
						minCompoundAmoeba = compoundAmoeba;
						bacillus = eachBacillus;
						masterAmoeba = bacillus.oddAmoeba;
						slaveAmoeba = bacillus.evenAmoeba;
					}
				}
			}
			assert bacillus != null;
			assert masterAmoeba != null && slaveAmoeba != null;

			CompoundBacillus compoundBacillus;
			assert (masterAmoeba.brotherAmoeba != null);

			Bacillus brotherBacillus = masterAmoeba.brotherAmoeba
					.getBacillus(slaveAmoeba);

			// TODO I am not sure that the method invoked above performs exactly
			// the same as we expect. May be we should use the cycle below.

			// Bacillus brotherBacillus = null;
			// for (Bacillus bacillus2 : bacilli) {
			// if (bacillus2.evenAmoeba == masterAmoeba.brotherAmoeba
			// && bacillus2.oddAmoeba == slaveAmoeba) {
			// brotherBacillus = bacillus2;
			// break;
			// }
			// if (bacillus2.oddAmoeba == masterAmoeba.brotherAmoeba
			// && bacillus2.evenAmoeba == slaveAmoeba) {
			// brotherBacillus = bacillus2;
			// break;
			// }
			// }
			if (brotherBacillus != null) {
				compoundBacillus = new CompoundBacillus(
						masterAmoeba.parentAmoeba, slaveAmoeba, bacillus,
						brotherBacillus);
				bacilli.remove(brotherBacillus);
			} else {
				compoundBacillus = new CompoundBacillus(
						masterAmoeba.parentAmoeba, slaveAmoeba, bacillus);
			}

			bacilli.remove(bacillus);
			bacilli.add(compoundBacillus);

		}

		rootBacillus = bacilli.get(0);
		assert rootBacillus.compoundBacillus == null;
	}

	public void addSimpleBacillus(SimpleBacillus simpleBacillus) {
		primitiveBacilli.add(simpleBacillus);
	}

	public void draw(Canvas canvas) {
		rootBacillus.draw(canvas);
	}

}
